using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using System.Collections.Generic;

namespace SP.SyspetroOcelot
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("API", new OpenApiInfo { Title = "Gateway Api", Version = "V1", Description = "Gateway Api" });
            });
            services.AddControllers();
            services.AddOcelot(Configuration).AddConsul();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSwagger();
            var Apps = Configuration.GetSection("Apps").Value.Split('_');

            app.UseSwaggerUI(c =>
            {
                foreach(string m in Apps)
                {
                    c.SwaggerEndpoint($"/swagger/ui/{m}", m + " api");
                }
                //Apps.ForEach(m =>
                //{
                //    c.SwaggerEndpoint($"/swagger/ui/{m}", m + " api");
                //});
            });
            app.UseOcelot();
        }
    }
}
